package utils.common;

import javax.annotation.concurrent.ThreadSafe;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * description: <br>
 * createTime: 2019/11/1215:39 <br>
 *
 * @author zzh
 */
@ThreadSafe
public class DateUtils {
    public static final int INT_0 = 0;

    public static final int INT_23 = 23;

    public static final int INT_59 = 59;

    public static final int INT_99 = 99;

    public static final long MILLISECONDS_A_DAY = 24 * 60 * 60 * 1000;

    public static final long MILLISECONDS_A_HOUR = 60 * 60 * 1000;

    private static final ThreadLocal<SimpleDateFormat> DATE_FORMAT = new ThreadLocal(){
        @Override
        protected Object initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd");
        }
    };

    private static final ThreadLocal<SimpleDateFormat> DATE_TIME_FORMAT = new ThreadLocal(){
        @Override
        protected Object initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
    };

    private static final ThreadLocal<SimpleDateFormat> DATE_HOUR_FORMAT = new ThreadLocal(){
        @Override
        protected Object initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH");
        }
    };

    private DateUtils(){}

    public static String commonDateFormat(Date date) {
        return DATE_FORMAT.get().format(date);
    }

    public static String commonDateFormat() {
        return DATE_FORMAT.get().format(new Date());
    }

    public static Date commonDateParse(String dateStr) throws ParseException {
        return DATE_FORMAT.get().parse(dateStr);
    }

    public static String commonDatetimeFormat(Date date) {
        return DATE_TIME_FORMAT.get().format(date);
    }

    public static String commonDatetimeFormat() {
        return DATE_TIME_FORMAT.get().format(new Date());
    }

    public static Date commonDatetimeParse(String dateStr) throws ParseException {
        return DATE_TIME_FORMAT.get().parse(dateStr);
    }

    public static Date commonDateHourParse(String dateStr) throws ParseException {
        return DATE_HOUR_FORMAT.get().parse(dateStr);
    }

    public static Date setDate2End(Date date) {
        Calendar calendar = Calendar.getInstance();
        if(date != null ) {
            calendar.setTime(date);
        }
        calendar.set(Calendar.HOUR_OF_DAY, INT_23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, INT_99);
        return calendar.getTime();
    }

    public static Date setTodayDate2End() {
        return setDate2End(null);
    }

    public static Date setDate2Zero(Date date) {
        Calendar calendar = Calendar.getInstance();
        if(date != null ) {
            calendar.setTime(date);
        }
        calendar.set(Calendar.HOUR_OF_DAY, INT_0);
        calendar.set(Calendar.MINUTE, INT_0);
        calendar.set(Calendar.SECOND, INT_0);
        calendar.set(Calendar.MILLISECOND, INT_0);
        return calendar.getTime();
    }

    public static Date setTodayDate2Zero () {
        return setDate2Zero(null);
    }

    public static Date setDateTime2Zero(Date date) {
        Calendar calendar = Calendar.getInstance();
        if(date != null ) {
            calendar.setTime(date);
        }
        calendar.set(Calendar.MINUTE, INT_0);
        calendar.set(Calendar.SECOND, INT_0);
        calendar.set(Calendar.MILLISECOND, INT_0);
        return calendar.getTime();
    }

    public static Date setNowDateTime2Zero () {
        return setDateTime2Zero(null);
    }

    public static String getDayChanges(Date currentTime, int plusDay){
        Calendar c = new GregorianCalendar();
        c.setTime(currentTime);
        c.add(Calendar.DATE,plusDay);
        currentTime=c.getTime();
        String changeMinute = DATE_FORMAT.get().format(currentTime);
        return changeMinute;
    }


    public static long timhour2Timestamp(String timehour) throws ParseException {
        return DATE_HOUR_FORMAT.get().parse(timehour).getTime();
    }

    public static String getHourChanges(Date currentTime, int plusHour){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH");
        Calendar c = new GregorianCalendar();
        c.setTime(currentTime);
        c.add(Calendar.HOUR,plusHour);
        currentTime=c.getTime();
        String changeMinute = df.format(currentTime);
        return changeMinute;
    }

    public static Date getYesterdayStart() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.add(Calendar.DAY_OF_MONTH, -1);
        return cal.getTime();
    }

    public static Date getLastHourStart() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.add(Calendar.HOUR, -1);
        return cal.getTime();
    }


    public static Date getMonthStart() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        return cal.getTime();
    }


    public static void main(String[] args) {
        System.out.println(getMonthStart());
    }
}
